// Description: changes page-layout over actions; possibiity to choose default page layout over options

// Author: Analogus
// History:
// Initial version: 2012-04-29
// 2012-05-02 Analogus: Added user-definable default page layout ; moved BookReload()-Function to Anlaogus_Viewer.js; 
// 2012-06-10 Mark Nord - UTF-8 encoding, var-defs, now working in BETA-folder


tmp = function() 
{
	var 
		L = Core.lang.getLocalizer("Analogus_Viewer"), 
		log = Core.log.getLogger("Analogus_Viewer"), 
		MSG = Core.ui.showMsg,  
		//optLayout = Analogus_Viewer.options.Default_Page_Layout,  
	
	//switches page-styles; calls appropr. function in kbook.xs:
	setPagestyleFunction = function (value)
		{	
			switch (value) 
			{
				case 'Value1': 
					pageSelectStyleOverlayModel.doMarginRemove();	
					break; 
				case 'Value2': 
					pageSelectStyleOverlayModel.doSplitPage2x2(); 
					break; 
				case 'Value3': 
					pageSelectStyleOverlayModel.doSplitPage2x3(); 
					break; 
				case 'Value4': 
					pageSelectStyleOverlayModel.doFitPage(); 
					break; 				
				default: 
					pageSelectStyleOverlayModel.doNormalPage();
			}
		}
		
	//avoids () in Core.events.subscribe:
	setPagestyleFunction2 = function()
		{
			try 
			{setPagestyleFunction(Analogus_Viewer.options.Default_Page_Layout)}
			catch (e)
			{
				//MSG('error in setPagestyleFunction2'); 
			}
		}
		
	//Analogus: reloads book if EPUB; used in ANALOGUS_CSS.js & EpubUserStyle.js; 
	BookReload = function ()
		{
			//optLayout = Analogus_Viewer.options.Default_Page_Layout; 
			current = kbook.model.currentBook;
			
			if (current && current.media.mime == 'application/epub+zip')
				{
					current.media.close(kbook.bookData);
					kbook.bookData.setData(null);
					password = (kbook.model.protectedBookInfo) ? kbook.model.protectedBookInfo.password : null; // only used on x50
					current.media.load(current.cache, kbook.model, kbook.model.onChangeBookCallback, password);
					//sets default page-layout:
					setPagestyleFunction2(); 
				}	

		};

//--------------------------------------------------------------------------------------------------------
	var Analogus_Viewer = 
	{
		name: "Analogus_Viewer",
		settingsGroup:"fonts", 
			
		optionDefs:
		[
			{
			name: 'Default_Page_Layout',	
			title: L('DEFAULT_PAGE_LAYOUT'),
			icon: 'BOOKOPEN', 
			helpText: L('HELP_DEFAULT_PAGE_LAYOUT'),
			defaultValue: 'NoOption',
			values: ['NoOption', 'Value1', 'Value2', 'Value3', 'Value4'],
			valueTitles: 
				{
				'NoOption': L('OPTION_NO_OPTION'), 
				'Value1': L('MARGIN_REMOVE'), 					
				'Value2': L('SPLIT_PAGE2X2'), 
				'Value3': L('SPLIT_PAGE2X3'), 
				'Value4': L('FIT_PAGE')
				}	
			},
		],
		
		
		actions: 
		[
			{
				name: "setPageStyle",
				title: L("setPageStyle"),
				group: "PRS_DEVELOPER_TOOLS",
				icon: "INFO",
				action: function () 
				{
					setPagestyleFunction2(); 
				}
			}, 
				
			{
				name: "test1",
				title: L("test1"),
				group: "PRS_DEVELOPER_TOOLS",
				icon: "INFO",
				action: function () 
				{
					MSG(Analogus_Viewer.options.Default_Page_Layout); 
				}
			}, 
			
			{
				name: "test2",
				title: L("test2"),
				group: "PRS_DEVELOPER_TOOLS",
				icon: "INFO",
				action: function () 
				{
					MSG(Analogus_Viewer.options.Default_Page_Layout); 
				}
			}
			
		], 
			
		onInit: function () 
			{
				//Wichtig: keine Funktionen mit () anhaengen sondern nur den Funktionsnamen einfuegen; 
				//deshalb werden die Parameter in setPagestyleFunction() uebergeben
				
				//loads default page-layout:
				Core.events.subscribe(Core.events.EVENTS.BOOK_CHANGED, setPagestyleFunction2); 
			},	
			
	/*		
		onSettingsChanged: function (propertyName, oldValue, newValue, object) 
		{
			switch (propertyName) 
			{
				case 'xxx':
						yyy (newValue) ;
				break; 
			}
		}
	*/

	};
	

	Core.addAddon(Analogus_Viewer);
	
};


try {
	tmp();
	} catch (e) {
		// Core's log
		log.error("in Analogus_Viewer.js", e);
	}